package org.insia.smartposse.mapping.dao;

import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.criterion.Expression;
import org.hibernate.criterion.Restrictions;
import org.insia.smartposse.manager.ProjectManager;
import org.insia.smartposse.manager.ProjectsListManager;
import org.insia.smartposse.manager.SkillManager;
import org.insia.smartposse.manager.list.JobsListManager;
import org.insia.smartposse.mapping.JobProject;
import org.insia.smartposse.mapping.base.BaseJobProjectDAO;


public class JobProjectDAO extends BaseJobProjectDAO implements org.insia.smartposse.mapping.dao.iface.JobProjectDAO {

	public JobProjectDAO () {}

	public JobProjectDAO (Session session) {
		super(session);
	}

	private Criteria critAll(Session session) {
		Criteria crit = session.createCriteria(getReferenceClass());
		if (null != getDefaultOrder())
			crit.addOrder(getDefaultOrder());
		return crit;
	}

	/**
	 * Method for obtains list of JobProject with several information :
	 * <ul>
	 * <li>Complete: if the job is complete or not</li>
	 * <li>Subject: word contains in Subject of Project</li>
	 * </ul>
	 *
	 * @param session
	 * @param complete
	 * @param subject
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public List<JobProject> search(Session session, int complete, ProjectManager project,
			SkillManager skill) {

		Criteria crit = critAll(session);

		// Complete or Not
		if (complete == JobsListManager.COMPLETE) {
			crit.add(Expression.isNotNull("Freelance"));
		}

		if (complete == ProjectsListManager.NOTCOMPLETE) {
			crit.add(Expression.isNull("Freelance"));
		}

		// Search with project
		if (null != project) {
			crit.add(Restrictions.eq("Project", project.getProject()));
		}

		//Search with Skill
		if (null != skill ){
			crit.add(Restrictions.eq("Skill", skill.getSkill()));
		}

		return crit.list();
	}

	/**
	 * Return the number of Job not complete for a specifique Project
	 *
	 * @param project
	 * @param session
	 * @return
	 */
	public int nbJobNotCompleteProjectSameProject(ProjectManager project, Session session) {
		return search(session, JobsListManager.NOTCOMPLETE, project, null).size();
	}


}